using System.Collections.Generic;
using UnityEngine;

public class BlackholeSkill : MonoBehaviour
{
    private Player player;
    [SerializeField] private GameObject hotkeyPrefab;
    [SerializeField] private List<KeyCode> keyCodes;
    private float maxSize;
    private float growSpeed;
    private float shrinkSpeed;
    private float blackholeTimer;
    private bool canGrow = true;
    private bool canShrink = false;
    private bool canCreateHotkeys = true;
    private bool cloneAttackReleased;
    private bool playerCanDisapear = true;
    private int amountOfAttacks;
    private float cloneAttackCooldown;
    private float cloneAttackTimer;
    private List<Enemy> targets = new();
    private List<GameObject> createHotkey = new();
    public bool canExitState { get; private set; }
    private SkillManager skillManager;

    private void Awake()
    {
        player = FindObjectOfType<Player>();
        skillManager = FindObjectOfType<SkillManager>();
    }

    private void Update()
    {
        cloneAttackTimer -= Time.deltaTime;
        blackholeTimer -= Time.deltaTime;
        if (blackholeTimer <= 0)
        {
            if (targets.Count > 0)
            {
                blackholeTimer = Mathf.Infinity;
                ReleaseCloneAttack();
            }
            else
            {
                FinishBlackholeAbility();
            }
        }
        if (Input.GetKeyDown(KeyCode.R))
        {
            ReleaseCloneAttack();
        }
        CloneAttackLogic();
        if (canGrow && !canShrink)
        {
            transform.localScale = Vector2.Lerp(transform.localScale, new Vector2(maxSize, maxSize), growSpeed * Time.deltaTime);
        }
        if (canShrink)
        {
            transform.localScale = Vector2.Lerp(transform.localScale, new Vector2(-1, -1), shrinkSpeed * Time.deltaTime);
            if (transform.localScale.x <= 0.1f)
            {
                DestroyHotkeys();
                Destroy(gameObject);
            }
        }
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.TryGetComponent(out Enemy enemy))
        {
            enemy.FreezeTime(true);
            CreateHotkey(other, enemy);
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.TryGetComponent(out Enemy enemy))
        {
            enemy.FreezeTime(false);
        }
    }

    private void CreateHotkey(Collider2D other, Enemy enemy)
    {
        if (keyCodes.Count == 0)
        {
            return;
        }
        if (!canCreateHotkeys)
        {
            return;
        }
        GameObject newHotkey = Instantiate(hotkeyPrefab, other.transform.position + new Vector3(0, 2), Quaternion.identity);
        createHotkey.Add(newHotkey);
        KeyCode choosenKey = keyCodes[Random.Range(0, keyCodes.Count)];
        _ = keyCodes.Remove(choosenKey);
        BlackholeHotkey newHotkeyController = newHotkey.GetComponent<BlackholeHotkey>();
        newHotkeyController.SetupHotkey(choosenKey, enemy.transform, this);
    }

    public void SetupBlackhole(float maxSize, float growSpeed, float shrinkSpeed, int amountOfAttacks = 4, float cloneAttackCooldown = 0.3f, float duration = 3f)
    {
        this.maxSize = maxSize;
        this.growSpeed = growSpeed;
        this.shrinkSpeed = shrinkSpeed;
        this.amountOfAttacks = amountOfAttacks;
        this.cloneAttackCooldown = cloneAttackCooldown;
        this.blackholeTimer = duration;
        if (skillManager.Clone.crystalInsteadOfClone)
        {
            playerCanDisapear = false;
        }
    }

    private void CloneAttackLogic()
    {
        if (cloneAttackTimer <= 0 && cloneAttackReleased && amountOfAttacks > 0)
        {
            cloneAttackTimer = cloneAttackCooldown;
            int randomIndex = Random.Range(0, targets.Count);
            float xOffset;
            if (Random.Range(0, 100) > 50)
            {
                xOffset = 2;
            }
            else
            {
                xOffset = -2;
            }
            if (skillManager.Clone.crystalInsteadOfClone)
            {
                skillManager.Crystal.Create();
                skillManager.Crystal.CurrentCrystalChooseRandomEnemy();
            }
            else
            {
                skillManager.Clone.Create(targets[randomIndex].transform, new Vector3(xOffset, 0));
            }
            amountOfAttacks--;
            if (amountOfAttacks <= 0)
            {
                Invoke("FinishBlackholeAbility", 1f);
            }
        }
    }

    private void FinishBlackholeAbility()
    {
        canExitState = true;
        canShrink = true;
        cloneAttackReleased = false;
    }

    private void ReleaseCloneAttack()
    {
        if (targets.Count <= 0)
        {
            return;
        }
        DestroyHotkeys();
        cloneAttackReleased = true;
        canCreateHotkeys = false;
        if (playerCanDisapear)
        {
            playerCanDisapear = false;
            player.Fx.MakeTransprent(true);
        }
    }

    private void DestroyHotkeys()
    {
        if (createHotkey.Count == 0)
        {
            return;
        }
        foreach (GameObject hotkey in createHotkey)
        {
            Destroy(hotkey);
        }
    }

    public void AddEnemyToList(Transform enemy)
    {
        if (enemy.TryGetComponent(out Enemy enemyComponent))
        {
            targets.Add(enemyComponent);
        }
    }

    public void AddEnemyToList(Enemy enemy)
    {
        targets.Add(enemy);
    }
}
